/**
 * @作者 Luke
 * @微信公号 欧诺的小书屋
 * @开源项目 $ http://7yue.pro
 * @免费专栏 $ http://course.7yue.pro
 * @我的课程 $ http://imooc.com/t/4294850
 * @创建时间 2020-6-23 20:24
 */
package io.github.talelin.latticy.controller.v1;

import io.github.talelin.latticy.dto.course.CreateOrUpdateHomeworkDTO;
import io.github.talelin.latticy.model.HomeworkDO;
import io.github.talelin.latticy.model.HomeworkItemDO;
import io.github.talelin.latticy.service.HomeworkService;
import io.github.talelin.latticy.vo.CreatedVO;
import io.github.talelin.latticy.vo.UpdatedVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.Positive;
import java.util.List;

@RestController
@RequestMapping("homework")
@Validated
public class HomeworkController {

    @Autowired
    private HomeworkService homeworkService;


    @GetMapping("all/{tid}/{cid}")
    public List<HomeworkDO> getHomework(@PathVariable(value = "tid") @Positive Long teacherId,
                                        @PathVariable(value = "cid") @Positive Long courseId){
        return this.homeworkService.getHomework(teacherId, courseId);
    }

    @GetMapping("detail/{hid}")
    public List<HomeworkItemDO> getDetail(@PathVariable(value = "hid") @Positive Long homeworkId){
        return this.homeworkService.getHomeworkDetail(homeworkId);
    }

    @PostMapping("distribute")
    public CreatedVO distributeHomework(@RequestBody CreateOrUpdateHomeworkDTO validator){
        this.homeworkService.distributeHomework(validator);
        return new CreatedVO();
    }

    @PostMapping("correction")
    public UpdatedVO correction(@RequestBody CreateOrUpdateHomeworkDTO validator){
        boolean correction = this.homeworkService
                .correction(validator.getHomeworkItemId(),
                        validator.getStatus(), validator.getScore());
        if (correction) {
            return new UpdatedVO("success");
        }else{
            return new UpdatedVO("error");
        }
    }
}
